//
//  ConvertKeySettingManager.m
//  ExcelToPlist
//
//  Created by 赵梁 on 2017/3/30.
//  Copyright © 2017年 赵梁. All rights reserved.
//

#import "ConvertKeySettingManager.h"

#import "AIFileManager.h"
#import "ConvertKeyModel.h"

@implementation ConvertKeySettingManager

+ (void)saveToDisc:(NSArray<ConvertKeyModel*>*)dataList
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataList];
    [AIFileManager setObject:data forKey:@"CovertKeyInfo.txt"];
}

+ (NSArray<ConvertKeyModel*>*)loadConfigFromDisc
{
    NSData *data = [AIFileManager objectForKey:@"CovertKeyInfo.txt"];
    if (data) {
        NSArray *list = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return list;
    }
    return nil;
}

+ (NSArray<ConvertKeyModel*>*)loadDefaultConvertKey
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ConvertedDefaultKey" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
    assert(dic);
    
    NSMutableArray *keyList = [NSMutableArray arrayWithCapacity:dic.allKeys.count];
    for (NSString *key in dic.allKeys) {
        ConvertKeyModel *keyModel = [ConvertKeyModel new];
        keyModel.keyOld = key;
        keyModel.keyNew = dic[key];
        
        [keyList addObject:keyModel];
    }
    
    [self saveToDisc:keyList];
    
    return keyList;
}

@end
